package com.luodim.dialogtest;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by luodi on 2017/7/7.
 */

public class MDialog extends Dialog {
    private Context mContext;
    private int mWidth;
    private int mHeight;
    private boolean mCancelTouchOut;
    private View mView;

    public MDialog(@NonNull Builder builder) {
        this(builder, 0);
    }

    public MDialog(@NonNull Builder builder, @StyleRes int resStyle) {
        super(builder.context, resStyle);
        init(builder);
    }

    protected MDialog(@NonNull Builder builder, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(builder.context, cancelable, cancelListener);
        init(builder);
    }

    private void init(Builder builder) {
        mContext = builder.context;
        mWidth = builder.width;
        mHeight = builder.height;
        mCancelTouchOut = builder.cancelTouchout;
        mView = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        setCanceledOnTouchOutside(mCancelTouchOut);

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = mWidth;
        params.height = mHeight;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
     }

    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightdp(int h) {
            height = (int) (h * getDensity());
            return this;
        }

        public Builder heightPercent(float percent) {
            height = (int) (getScreenHeight() * percent);
            return this;
        }

        public Builder widthdp(int w) {
            width = (int) (w * getDensity());
            return this;
        }

        public Builder widthPercent(float percent) {
            width = (int) (getScreenWidth() * percent);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }


        public MDialog build() {
            if (resStyle != -1) {
                return new MDialog(this, resStyle);
            } else {
                return new MDialog(this);
            }
        }

        private float getDensity() {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(metrics);
            return metrics.density;
        }

        private float getScreenHeight() {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(metrics);
            return metrics.heightPixels;
        }

        private float getScreenWidth() {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(metrics);
            return metrics.widthPixels;
        }
    }
}